package com.zq.it.cms.support;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.zq.it.cms.common.Context;
import com.zq.it.cms.domain.User;

//import com.qianyi.core.domain.Global;

/**
 * ForeInterceptor
 * 
 * @author liufang
 */
public class ForeInterceptor implements HandlerInterceptor {

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		User shiroUser = null;
		Subject subject = SecurityUtils.getSubject();
		Object principal = subject.getPrincipal();
		// 用户登录信息，允许记住用户。
		if (principal != null) {
			if (principal instanceof User) {
				shiroUser = (User) principal;
			} else {
				subject.logout();
			}
		}
		if (shiroUser != null) {
			Context.setCurrentUser(shiroUser);
		} else {
			// 未登录，组织为空
		}
		return true;
	}

	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
	}

	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
		Context.resetCurrentUser();
	}
	
}
